package gnv;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

import com.swtdesigner.SWTResourceManager;

public class BackupGUI {

	protected Shell shell;

	/**
	 * Launch the application
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			BackupGUI window = new BackupGUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window
	 */
	public void open() {
		final Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	/**
	 * Create contents of the window
	 * @throws  
	 */
	protected void createContents()   {
		shell = new Shell(SWT.TITLE | SWT.BORDER);
		shell.setLayout(new FormLayout());
		shell.setImage(SWTResourceManager.getImage(BackupGUI.class, "/gnv/genoa.png"));
		shell.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
		shell.setText("Backup");

		
		int mWidth = 284;
		int mHeight = 140;
	 	Rectangle pRec = Display.getCurrent().getClientArea();
	 	int nLeft = (pRec.width - mWidth) / 2;
	 	int nTop = (pRec.height - mHeight) / 2;
		shell.setBounds(nLeft, nTop, mWidth, mHeight);

		
		final Composite composite = new Composite(shell, SWT.NONE);
		composite.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
		final FormData fd_composite = new FormData();
		fd_composite.bottom = new FormAttachment(100, -5);
		fd_composite.right = new FormAttachment(100, -5);
		fd_composite.top = new FormAttachment(0, 5);
		fd_composite.left = new FormAttachment(0, 5);
		composite.setLayoutData(fd_composite);
		composite.setLayout(new FormLayout());

		final Label label = new Label(composite, SWT.WRAP);
		label.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
		final FormData fd_label = new FormData();
		fd_label.bottom = new FormAttachment(0, 50);
		fd_label.right = new FormAttachment(100, -5);
		fd_label.top = new FormAttachment(0, 5);
		fd_label.left = new FormAttachment(0, 5);
		label.setLayoutData(fd_label);
		label.setText("Aguarde un instante mientras se realiza la copia de seguridad.");

		final ProgressBar progressBar = new ProgressBar(composite, SWT.NONE);
		final FormData fd_progressBar = new FormData();
		fd_progressBar.left = new FormAttachment(0, 5);
		fd_progressBar.right = new FormAttachment(100, -5);
		fd_progressBar.bottom = new FormAttachment(label, 25, SWT.BOTTOM);
		fd_progressBar.top = new FormAttachment(label, 5, SWT.BOTTOM);
		progressBar.setLayoutData(fd_progressBar);
		
		
		
			Display.getCurrent().asyncExec(new Runnable() {
				public void run() {
					if (progressBar.isDisposed())
						return;
					try {
						int tool = new Herramientas().DailyBackup();
						System.out.println(tool);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
					 for (int i = 0; i < 100; i++) {
				          progressBar.setSelection(i + 1);
				          try {
				            Thread.sleep(50);
				          } catch (Throwable e) {
				          }
				        }
					shell.close();
				}
			});

	}

}
